<template>
  <div class="login">
    <h1>欢迎登陆</h1>
    <div class="form">
      <div class="item">
        <span class="iconfont icon-shoujihao"></span>
        <input type="text" placeholder="请输入手机号" v-model="phone" />
      </div>
      <div class="item">
        <span class="iconfont icon-mima"></span>
        <input type="password" placeholder="请输入密码" v-model="password" />
      </div>
      <div class="item last">
        <span class="iconfont icon-yanzhengma"></span>
        <input type="text" placeholder="请输入验证码" v-model="captcha" />
        <button @click="getCode">发送</button>
      </div>
      <div class="item">
        <div class="submit" @click="submit">立即登陆</div>
      </div>
    </div>
  </div>
</template>

<script>
import { Notify } from "vant";
import {
  getCaptcha,
  getCaptchaVerify,
  getLoginCellphone,
  
} from "../../api/base.js";
export default {
  data() {
    return {
      show: false,
      content: null,
      phone: "",
      code: "",
      captcha: "",
      password: null,
    };
  },
  methods: {
    // 1.验证手机号
    getCode() {
      let reg =
        /^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/;
      if (!reg.test(this.phone)) {
        Notify({ type: "warning", message: "请输入正确手机号码" });
        return;
      }

      // 请求验证码接口
      getCaptcha({ phone: this.phone }).then((data) => {
        console.log(data);
        if (data.data) {
          Notify({ type: "success", message: "验证码发送成功" });
        } else {
          Notify({ type: "danger", message: "验证码发送失败" });
        }
      });
    },
   
    submit() {
      // window.localStorage.setItem("login","true");
      // // 个人中心判断是否登录
      // // this.$router.push("/userInfo")
      // // 必须登录才能使用
      // if(window.localStorage.getItem("guidePage")){
      //     this.$router.push("/recommend")
      // }else{
      //     this.$router.push("/guidePage")
      // }
      console.log(12345678);

      let reg = /^[0-9]{4}$/;
      if (!reg.test(this.captcha)) {
        Notify({ type: "warning", message: "请输入正确验证码" });
        return;
      }

      getCaptchaVerify({
        phone: this.phone,
        captcha: this.captcha,
      })
        .then((data) => {
          console.log(data);
          if (data.code == 200) {
            getLoginCellphone({
              password: this.password,
              phone: this.phone,
            }).then((info) => {
              console.log("登录状态",info);

 
              Notify({ type: "success", message: "登录成功" });
              window.localStorage.setItem("uid",info.account.id)
              window.localStorage.setItem("login", info.cookie);
              window.localStorage.setItem("token", info.token);
              // window.localStorage.setItem("cookie", info.cookie);
              // 个人中心判断是否登录
              // this.$router.push("/userInfo")
              // 必须登录才能使用
              if (window.localStorage.getItem("guidePage")) {
                this.$router.push("/recommend");
              } else {
                this.$router.push("/guidePage");
              }
            });
          } else {
            Notify({ type: "danger", message: "登录失败" });
          }
        })
        .catch(function () {
          Notify({ type: "danger", message: "登录失败" });
        });
    },
  },

};
</script>

<style lang="less" scoped>
.van-popup {
  width: 70%;
  min-height: 80px;
  border-radius: 14px;
  line-height: 20px;
  text-align: center;
  font-size: 18px;
  color: #d4473c;
  font-weight: 700;
  vertical-align: middle;
  padding-top: 20px;
}
.login {
  background-color: #d4473c;
  width: 100%;
  height: 100%;
}
h1 {
  font-size: 48px;
  text-align: center;
  height: 48px;
  padding: 120px 0 55px 0;
  color: #fff;
}
.form {
  margin: 0 auto;
  width: 80%;
  .item {
    position: relative;
    span {
      font-size: 20px;
      position: absolute;
      top: 9px;
      left: 6px;
    }
    input {
      width: 100%;
      height: 40px;
      line-height: 40px;
      border-radius: 30px;
      margin-bottom: 12px;
      outline: none;
      border: none;
      text-indent: 30px;
      margin-bottom: 20px;
      &::placeholder {
        color: #ccc;
      }
    }
  }
  .last {
    position: relative;
    button {
      width: 80px;
      height: 40px;
      position: absolute;
      top: 0;
      right: 0;
      outline: none;
      border: none;
      background-color: #eee;
      border-radius: 30px;
      color: #666;
    }
  }
  .submit {
    width: 60%;
    height: 40px;
    margin: 30px auto;
    line-height: 40px;
    font-size: 18px;
    background-color: #eee;
    text-align: center;
    color: #d4473c;
    font-weight: 700;
    border-radius: 15px;
  }
  .coms {
    color: #fff;
    width: 80px;
    margin: 0 auto;
  }
}
</style>